package com.zx._02_thread.基本操作;

import com.zx.common.util.ThreadUtil;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class 依次打印数字 {

    //3个线程 第一个线程打印123然后第二个线程打印456然后第三个打印789以此类推，一直打到75
    static int num = 1;
    static ExecutorService pool = Executors.newFixedThreadPool(10);
    static volatile boolean a = true;
    static volatile boolean b = false;
    static volatile boolean c = false;

    public static void main2(final String[] args) {
        final AtomicBoolean bool = new AtomicBoolean(false);
        for (int j = 0; j < 3; j++) {
            pool.execute(() -> {
                while (true) {
                    if (num >= 75) {
                        break;
                    }
                    if (!bool.compareAndSet(false, true)) {
                        continue;
                    }
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    ThreadUtil.sleepMill(500);
                    bool.compareAndSet(true, false);
                }
            });
        }
        pool.shutdown();
    }

    public static void main3(final String[] args) {

        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if (a) {
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    a = false;
                    b = true;
                }
            }
        });

        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if (b) {
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    b = false;
                    c = true;
                }
            }
        });

        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if (c) {
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    c = false;
                    a = true;
                }
            }
        });

        pool.shutdown();
    }


    public static void main(final String[] args) {
        final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        queue.add("a");
        queue.add("b");
        queue.add("c");

        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if ("a".equals(queue.peek())) {
                    if (num >= 75) {
                        break;
                    }
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    queue.poll();
                    queue.add("a");
                }
            }
        });
        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if ("b".equals(queue.peek())) {
                    if (num >= 75) {
                        break;
                    }
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    queue.poll();
                    queue.add("b");
                }
            }
        });
        pool.execute(() -> {
            while (true) {
                if (num >= 75) {
                    break;
                }
                if ("c".equals(queue.peek())) {
                    if (num >= 75) {
                        break;
                    }
                    for (int i = 0; i < 5; i++) {
                        log.info("{}", num);
                        num++;
                    }
                    queue.poll();
                    queue.add("c");
                }
            }
        });
        pool.shutdown();


    }
}
